<?php


namespace apps\marriage\core\model;


use vm\com\BaseModel;

class CourseOrder extends BaseModel
{
    const PAY_STATUS_UNPAIED = 10;  // 未付款
    const PAY_STATUS_PAIED = 20;    // 已付款

    const ORDER_STATUS_CONFIRMED = 10;  // 已确认
    const ORDER_STATUS_STUDYING = 30;  // 上课中
    const ORDER_STATUS_CANCELLED = 50;  // 已取消
    const ORDER_STATUS_REFUND_AUDITING = 80;  // 退款审核中
    const ORDER_STATUS_REFUNDING = 85;  // 退款中
    const ORDER_STATUS_REFUNDED = 90;  // 已退款
    const ORDER_STATUS_FINISHED = 200;  // 已完成

    public static function getCoursePayStatuses()
    {
        return [
            self::PAY_STATUS_UNPAIED => [
                'value' => self::PAY_STATUS_UNPAIED,
                'label' => '待付款'
            ],
            self::PAY_STATUS_PAIED => [
                'value' => self::PAY_STATUS_PAIED,
                'label' => '已付款'
            ]
        ];
    }

    public static function getCoursePayStatusLabel($status)
    {
        $statusList = self::getCoursePayStatuses();
        return isset($statusList[$status]) ? $statusList[$status]['label'] : '';
    }

    public static function getCourseOrderStatuses()
    {
        return [
            self::ORDER_STATUS_CONFIRMED => [
                'value' => self::ORDER_STATUS_CONFIRMED,
                'label' => '已确认'
            ],
            self::ORDER_STATUS_STUDYING => [
                'value' => self::ORDER_STATUS_STUDYING,
                'label' => '上课中'
            ],
            self::ORDER_STATUS_CANCELLED => [
                'value' => self::ORDER_STATUS_CANCELLED,
                'label' => '已取消'
            ],
            self::ORDER_STATUS_REFUND_AUDITING => [
                'value' => self::ORDER_STATUS_REFUND_AUDITING,
                'label' => '退款审核中'
            ],
            self::ORDER_STATUS_REFUNDING => [
                'value' => self::ORDER_STATUS_REFUNDING,
                'label' => '退款打款中'
            ],
            self::ORDER_STATUS_REFUNDED => [
                'value' => self::ORDER_STATUS_REFUNDED,
                'label' => '已退款'
            ],
            self::ORDER_STATUS_FINISHED => [
                'value' => self::ORDER_STATUS_FINISHED,
                'label' => '已完成'
            ]
        ];
    }

    public static function getCourseOrderStatusLabel($status)
    {
        $statusList = self::getCourseOrderStatuses();
        return isset($statusList[$status]) ? $statusList[$status]['label'] : '';
    }

    public function counts(array $params)
    {
        $query = $this;
        foreach ($params as $key => $val) {
            if($key == 'order_begin_time') {
                $query = $query->where('dateline', '>=', $val);
            } else if($key == 'order_end_time') {
                $query = $query->where('dateline', '<=', $val);
            } else {
                if(is_array($val)) {
                    $query = $query->where($key, 'IN', $val);
                } else {
                    $query = $query->where($key, $val);
                }
            }
        }
        return $query->count();
    }
}